﻿using MediatR;
using SheepCultivation.Common.Domain.RBAC;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;

namespace SheepCultivation.API.AddliCation.RBAC
{
    public class UserSeachHandler : IRequestHandler<UserSeachCommon, Result>
    {
        public readonly IBaseRepository<User> UserRepsoitory;

        public UserSeachHandler(IBaseRepository<User> userRepsoitory)
        {
            UserRepsoitory = userRepsoitory;
        }

        public async Task<Result> Handle(UserSeachCommon request, CancellationToken cancellationToken)
        {
            var res = new Result() { Code = (int)CommonEnumCode.error };
            try
            {
                var result = UserRepsoitory.GetAll().Where(x => x.UserName == request.UserName && x.Password == request.Password).ToList();
                if (result.Count() > 0)
                {
                    res.Code = (int)CommonEnumCode.success;
                    var list = UserRepsoitory.GetAll().Where(x => x.UserName == request.UserName && x.Password == request.Password).FirstOrDefault().UserId;
                    var list1 = UserRepsoitory.GetAll().Where(x => x.UserId == list);
                    res.Data = list1;
                    res.Msg = "用户显示";
                }
                else
                {
                    res.Code = (int)CommonEnumCode.error;
                }


                


            }
            catch (Exception)
            {
                throw;
            }
            return res;
        }
    }
}
